<?php
define('DESIRED_IMAGE_WIDTH', 	130);
define('DESIRED_IMAGE_HEIGHT', 	130);
class CI_Crop {
  //----------------------------------------------------------------
  // Crop-to-fit PHP-GD
  // Revision 2 [2009-06-01]
  // Corrected aspect ratio of the output image
  //----------------------------------------------------------------

  
	function thumbSquare($srcFileName, $dstFileName, $intWidth=DESIRED_IMAGE_WIDTH, $intHeight=DESIRED_IMAGE_HEIGHT) {		
		//
		// Add file validation code here
		//

		list( $source_width, $source_height, $source_type ) = getimagesize( $srcFileName );
		
		switch ( $source_type )
		{
			case IMAGETYPE_GIF:
				$source_gdim = imagecreatefromgif( $srcFileName );
				break;

			case IMAGETYPE_JPEG:
				$source_gdim = imagecreatefromjpeg( $srcFileName );
				break;

			case IMAGETYPE_PNG:
				$source_gdim = imagecreatefrompng( $srcFileName );
				break;
		}
		if(!$source_gdim)
			return false;
		$source_aspect_ratio = $source_width / $source_height;
		$desired_aspect_ratio = $intWidth / $intHeight;
		if ( $source_aspect_ratio > $desired_aspect_ratio )
		{
			//
			// Triggered when source image is wider
			//
			$temp_height = $intHeight;
			$temp_width = ( int ) ( $intHeight * $source_aspect_ratio );
		}
		else
		{
			//
			// Triggered otherwise (i.e. source image is similar or taller)
			//
			$temp_width = $intWidth;
			$temp_height = ( int ) ( $intWidth / $source_aspect_ratio );
		}

		//
		// Resize the image into a temporary GD image
		//
		$temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
		imagecopyresampled(
							$temp_gdim,
							$source_gdim,
							0, 0,
							0, 0,
							$temp_width, $temp_height,
							$source_width, $source_height
						);

		//
		// Copy cropped region from temporary image into the desired GD image
		//

		$x0 = ( $temp_width - $intWidth ) / 2;
		$y0 = ( $temp_height - $intHeight ) / 2;

		$desired_gdim = imagecreatetruecolor( $intWidth, $intHeight );
		imagecopy(
					$desired_gdim,
					$temp_gdim,
					0, 0,
					$x0, $y0,
					$intWidth, $intHeight
		);

		//
		// Render the image
		// Alternatively, you can save the image in file-system or database
		//
		//header( 'Content-type: image/jpeg' );
		imagejpeg( $desired_gdim, $dstFileName, 100);
		imagedestroy ( $source_gdim );
		imagedestroy ( $desired_gdim );
		//
		// Add clean-up code here
		//
		return true;
	}
	public static function cropImage($src_file, $target_file, $x, $y, $width, $height)
   	{
      list($width_orig, $height_orig, $type) = getimagesize($src_file);

      if ($width_orig == 0 || $height_orig == 0) {
         return false;
      }
     
      if ($type == IMG_JPG) {
         $image = @imagecreatefromjpeg($src_file);
      }
      else if ($type == IMG_PNG || $type == 3) { // php bug seemingly..
         $image = @imagecreatefrompng($src_file);
      }
      else if ($type == IMG_GIF) {
         $image = @imagecreatefromgif($src_file);
      }
      else {
         return false;
      }
      if (!$image) {
         return false;
      }
      $x = abs(intval($x));
      $y = abs(intval($y));
      $width = abs(intval($width));
      $height = abs(intval($height));
     
      if ( $width == 0 || $height == 0 || (($x + $width) > $width_orig) || (($y + $height) > $height_orig) ) {
         return false;
      }
     
      $image_p = imagecreatetruecolor($width, $height);
      if ( !imagecopy($image_p, $image, 0, 0, $x, $y, $width, $height) ) {
         return false;
      }
      return imagejpeg($image_p, $target_file, 100);
   	}
	function thumbCover($srcFileName, $dstFileName,$x0,$y0,$intWidth=DESIRED_IMAGE_WIDTH, $intHeight=DESIRED_IMAGE_HEIGHT) {		
		//
		// Add file validation code here
		//

		list( $source_width, $source_height, $source_type ) = getimagesize( $srcFileName );
		
		switch ( $source_type )
		{
			case IMAGETYPE_GIF:
				$source_gdim = imagecreatefromgif( $srcFileName );
				break;

			case IMAGETYPE_JPEG:
				$source_gdim = imagecreatefromjpeg( $srcFileName );
				break;

			case IMAGETYPE_PNG:
				$source_gdim = imagecreatefrompng( $srcFileName );
				break;
		}
		if(!$source_gdim)
			return false;
		$source_aspect_ratio = $source_width / $source_height;
		$desired_aspect_ratio = $intWidth / $intHeight;
		if ( $source_aspect_ratio > $desired_aspect_ratio )
		{
			//
			// Triggered when source image is wider
			//
			$temp_height = $intHeight;
			$temp_width = ( int ) ( $intHeight * $source_aspect_ratio );
		}
		else
		{
			//
			// Triggered otherwise (i.e. source image is similar or taller)
			//
			$temp_width = $intWidth;
			$temp_height = ( int ) ( $intWidth / $source_aspect_ratio );
		}

		//
		// Resize the image into a temporary GD image
		//
		$temp_gdim = imagecreatetruecolor( $source_width, $source_height );
		imagecopyresampled(
							$temp_gdim,
							$source_gdim,
							0, 0,
							0, 0,
							$temp_width, $temp_height,
							$source_width, $source_height
						);

		//
		// Copy cropped region from temporary image into the desired GD image
		//

		/*$x0 = ( $temp_width - $intWidth ) / 2;
		$y0 = ( $temp_height - $intHeight ) / 2;*/

		$desired_gdim = imagecreatetruecolor( $intWidth, $intHeight );
		imagecopy(
					$desired_gdim,
					$temp_gdim,
					0, 0,
					$x0, $y0,
					$intWidth, $intHeight
		);

		//
		// Render the image
		// Alternatively, you can save the image in file-system or database
		//
		//header( 'Content-type: image/jpeg' );
		imagejpeg( $desired_gdim, $dstFileName, 100);
		imagedestroy ( $source_gdim );
		imagedestroy ( $desired_gdim );
		//
		// Add clean-up code here
		//
		return true;
	}
	
}
?>